package com.bkgy.modular.tbdecsionrule.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.modular.tbdecsionrule.dao.TbDsjPlancheckThresholdMapper;
import com.bkgy.modular.tbdecsionrule.dto.TbDsjPlancheckThresholdDto;
import com.bkgy.modular.tbdecsionrule.entity.TbDsjPlancheckThreshold;
import com.bkgy.modular.tbdecsionrule.service.TbDsjPlancheckThresholdService;
import com.bkgy.sys.common.entity.ResultData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author
 * @version 1.0
 * @ClassName TbDsjPlancheckThresholdServiceImpl
 * @date 2023/3/28 16:15
 */
@Service
public class TbDsjPlancheckThresholdServiceImpl extends ServiceImpl<TbDsjPlancheckThresholdMapper, TbDsjPlancheckThreshold> implements TbDsjPlancheckThresholdService {
    @Resource
    private TbDsjPlancheckThresholdMapper tbDsjPlancheckThresholdMapper;

    @Override
    public ResultData findPlanCheckThrsholdData(JSONObject jsonObject) {
        Integer pageIndex = jsonObject.getInteger("pageIndex");
        Integer pageSize = jsonObject.getInteger("pageSize");
        String productionLine = jsonObject.getString("productionLine");
        if (pageIndex == null || pageSize == null) {
            return ResultData.ResultDataFaultSelf("请检查分页参数是否正确",null);
        }
        PageHelper.startPage(pageIndex,pageSize);
        LambdaQueryWrapper<TbDsjPlancheckThreshold> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(productionLine)) {
            queryWrapper.eq(TbDsjPlancheckThreshold::getProduction_line,productionLine);
        }
        List<TbDsjPlancheckThreshold> tbDsjPlancheckThresholds = tbDsjPlancheckThresholdMapper.selectList(queryWrapper);
        PageInfo<TbDsjPlancheckThreshold> pageInfo = new PageInfo<>(tbDsjPlancheckThresholds);
        return ResultData.ResultDataFaultSelf("操作成功",pageInfo);
    }

    @Override
    public ResultData updatePlanCheckThrsholdData(TbDsjPlancheckThresholdDto tbDsjPlancheckThresholdDto) {
        // 接收参数
        Double id = tbDsjPlancheckThresholdDto.getId();
        if (id == null) {
            return ResultData.ResultDataFaultSelf("id为空",null);
        }
        // 创建查询构造器
        LambdaQueryWrapper<TbDsjPlancheckThreshold> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TbDsjPlancheckThreshold::getId,id);
        TbDsjPlancheckThreshold tbDsjPlancheckThreshold = tbDsjPlancheckThresholdMapper.selectOne(queryWrapper);
        if (tbDsjPlancheckThreshold == null) {
            return ResultData.ResultDataFaultSelf("数据不存在",null);
        }
        BeanUtils.copyProperties(tbDsjPlancheckThresholdDto,tbDsjPlancheckThreshold);
        tbDsjPlancheckThresholdMapper.update(tbDsjPlancheckThreshold,queryWrapper);
        return ResultData.ResultDataFaultSelf("修改成功",null);
    }
}
